﻿Chenxin.UI.CustomerManage = Chenxin.extend(Chenxin.UI.PageCommon, {
    autoInitForm: false,
    actionUrl: 'service/customerMsgr.ashx',
    createGrid: function() {
        return new Chenxin.UI.Pagination({
            actionUrl: this.actionUrl + '?op=getlist',
            id: 'customerlist',
            renderTo: 'house_table'
        });
    },
    loadCustomerInfo: function() {
        var _this = this;
        Chenxin.ajax({
            type: "get",
            dataType: "text",
            paramType: 'json',
            url: this.actionUrl,
            data: { op: 'getcustomerinfo', demandid: this.currentId },
            onSuccess: function(data, msg) {
                if (data.demandinfo) {
                    _this.currentCustomerInfo = data.demandinfo;
                    if (data.baseinfo) {
                        _this.currentBaseInfo = data.baseinfo;
                    }
                    _this.renderCustomerInfo();
                }
                _this.createTraceList(data.countTrace);
            }
        });
    },
    renderCustomerInfo: function() {
        var m = this.currentCustomerInfo;
        var demandkind = "";
        if (m.DemandKind == "10")
            demandkind = "求购";
        else
            demandkind = "求租";
        var demandtype = "";
        if (m.DemandType == "10")
            demandtype = "住房";
        else if (m.DemandType == "20")
            demandtype = "商铺";
        else
            demandtype = "办公";

        var infoStr = '';
        infoStr += '<tr id="tr-house-info"><td colspan="22" class="gendan_box" style="height:81px;width:90%">';
        infoStr += '<div class=" fl">';
        infoStr += '<ul class="padding5">';
        infoStr += '<li style="float:left;width:140px;">';

        infoStr += '<input name="btn_trace" type="button" class="btn_pon magbotm5" value="我要跟单"/>';
        infoStr += '<input type="button" name="btnSetType"  class="btn_pon magbotm5" value="设置类别" />';
        infoStr += '<input type="button" name="btnAddFavorite"  class="btn_pon magbotm5" value="添加收藏" />';

        var strStatus;
        if (m.Status == 1) {
            strStatus = "记录无效";
        } else if (m.Status == -1) {
            strStatus = "记录有效";
        }
        infoStr += '<input name="btnSetStatus" type="button" class="btn_pon magbotm5" value="' + strStatus + '"/>';
        infoStr += '<input name="btn_viewownerinfo" type="button" class="btn_pon_4" value="查看客户信息"/>';
        infoStr += '<input type="button" name="btnEditBaseInfo"  class="btn_pon_4" value="编辑基本信息" />';
        infoStr += '<input type="button" name="btnEditDemandInfo"  class="btn_pon_4" value="编辑需求信息" />';
        infoStr += '<input type="button" name="btnChgPubLevel"  class="btn_pon_4" value="变更公开级别" />';
        infoStr += '</li>';
        infoStr += '</ul>';
        infoStr += '</div>';
        infoStr += '<div class="gdtab"><div class="outborder">';
        infoStr += '<div class="infos intitss" style="width:640px">';
        infoStr += '<span class="rb" style="width:380px"><b>跟单内容</b></span>';
        infoStr += '<span class="rbpsn" style="width:120px"><b>跟单人</b></span>';
        infoStr += '<span class="rbtim" style="width:120px"><b>跟单时间</b></span>';
        infoStr += '</div>';
        infoStr += '<div id="housetrace_table" style="width:650px"></div>';
        infoStr += '</div></div></td></tr>';
        $('#' + this.currentId).after(infoStr);
    },
    createTraceList: function(count) {
        if (!this.tracePage) {
            this.tracePage = new Chenxin.UI.Pagination({
                id: 'customertrace',
                actionUrl: this.actionUrl + '?op=getcustomertracelist',
                data: { customerid: this.currentCustomerInfo.CustomerId },
                renderTo: 'housetrace_table',
                pageSize: 4,
                width: 650
            });
        } else {
            this.tracePage.recordCount = 0;
            this.tracePage.hasCount = false;
            this.tracePage.reload({ customerid: this.currentCustomerInfo.CustomerId });
        }
    },
    initEvent: function() {
        var _this = this;
        //查询菜单点击展开事件
        $(".arrow").click(function() {
            $("#panel").slideToggle("slow");
            $(this).toggleClass("up"); return false;
        });
        $('.tr_houselist').live('click', function() {
            $('.tr_houselist,.selected_orgbgcolor').each(function() {
                $(this).removeClass('selected_orgbgcolor');
            });

            $(this).toggleClass('selected_orgbgcolor');
            if (!$('#tr-house-info').length) {
                _this.currentId = this.id;
                _this.loadCustomerInfo();
            } else {
                $('#tr-house-info').remove();
                if (_this.currentId != this.id) {
                    _this.currentId = this.id;
                    _this.loadCustomerInfo();
                }
            }
        });
        $("input[name='btn_viewownerinfo']").live('click', function() {
            _this.showCustomerInfo();
        });
        $("input[name='btn_trace']").live('click', function() {
            _this.showCurrentTraceWin();
        });
        /****************事件：设置类别**************/
        $("input[name='btnSetType']").live("click", function() {
            _this.setCustomerType();
        });
        /****************事件：客户基本信息编辑**************/
        $("input[name='btnEditBaseInfo']").live("click", function() {
            _this.editBaseInfo();
        });
        /****************事件：编辑客户需求信息**************/
        $("input[name='btnEditDemandInfo']").live("click", function() {
            _this.editDemandInfo();
        });
        /********************事件：添加收藏*****************/
        $("input[name='btnAddFavorite']").live("click", function() {
            _this.addFavorite();
        });
        /****************事件：底部按钮点击事件**************/
        $("input[name='btnSetStatus']").live("click", function() {
            if (confirm('确认要改变记录状态吗？')) {
                var objBt = this;
                Chenxin.ajax({
                    type: "get",
                    dataType: "text",
                    paramType: 'json',
                    url: _this.actionUrl,
                    data: { op: 'updstatus', demandid: _this.currentCustomerInfo.DemandId, status: _this.currentCustomerInfo.Status * (-1) },
                    onSuccess: function(data, msg) {
                        alert(msg);
                        var strVal;

                        if (_this.currentCustomerInfo.Status == 1) {
                            _this.currentCustomerInfo.Status = -1;
                            $(objBt).parents("tr:first").prev().addClass("clsInvalid");
                            strVal = "记录有效";
                        } else if (_this.currentCustomerInfo.Status == -1) {
                            _this.currentCustomerInfo.Status = 1;
                            $(objBt).parents("tr:first").prev().removeClass("clsInvalid");
                            strVal = "记录无效";
                        }
                        if (data.permit || msg == '状态变更成功') {
                            $("input[name='btnSetStatus']").val(strVal);
                        }
                    }
                });
            }
        });
        /********************事件：变更公开级别*****************/
        $("input[name='btnChgPubLevel']").live("click", function() {
            _this.chgPubLevel();
        });
    },
    showCustomerInfo: function() {
        var _this = this;
        if (!this.customerForm) {
            this.customerForm = new Chenxin.UI.CustomerForm({
                alwaysCheckPermit: true,
                scope: this,								
				showTraceWin: function() {
					var _this = this;
					this.scope.createTraceWin.call(this.scope);
					this.scope.currentTraceWin.show();
					$('#tr_noticetime').hide();
					$('#tr_noticecontent').hide();
					var _data = {};
					Chenxin.apply(_data, this.currentCustomerInfo);
				    _data.HouseCode = this.currentCustomerInfo.CustomerCode;
					_data.AddPerson = this.currentCustomerInfo.OprPerson;
					_data.AddDepart = this.currentCustomerInfo.OprDepart;
					this.scope.currentTraceWin.fitData(_data);
					this.scope.currentTraceWin.setTitle('客源跟单');

				},
                beforeFitData: function(model) {
                    _this.customerForm.currentCustomerInfo = model;
                }
            });
        }
        if (this.customerForm.show('view', { currentId: this.currentCustomerInfo.CustomerId })) {
            this.getModel(this.currentCustomerInfo.CustomerId, this.customerForm.fitData, this.customerForm, this.customerForm.actionUrl);
        }
        this.customerForm.setDisable();
        this.customerForm.setTitle('查看客户信息');
    },
    setCustomerType: function() {
        if (!this.CatalogForm) {
            this.CatalogForm = new Chenxin.UI.CatalogForm({
                scope: this,
                afterGetFormJson: function(json) {
                    //json.CustomerId = this.scrop.currentCustomerInfo.CustomerId;
                    json.DemandId = this.currentCustomerInfo.DemandId;
                }
            });
            this.CatalogForm.reset();
            this.CatalogForm.fitData({ Catalog: this.currentCustomerInfo.Catalog });
            this.CatalogForm.show(this.CatalogForm.op);
        } else {
            this.CatalogForm.reset();
            this.CatalogForm.fitData({ Catalog: this.currentCustomerInfo.Catalog });
            this.CatalogForm.show(this.CatalogForm.op);
        }
        $(".customerName").text(this.currentCustomerInfo.CustomerName);
    },
    editBaseInfo: function() {
        var _this = this;
        if (!this.currentCustomerInfo.HouseCode)
            delete this.currentCustomerInfo.HouseCode
        Chenxin.ajax({
            type: "get",
            dataType: "text",
            paramType: 'json',
            url: "service/customerMsgr.ashx",
            data: { op: 'checkcustomerpopwind', model: this.currentCustomerInfo },
            onSuccess: function(data, msg) {
                if (data.permit) {
                    var strUrl = "CustomerInfoAdd.aspx?infoclass=baseInfo";
                    strUrl = strUrl + "&customerid=" + _this.currentCustomerInfo.CustomerId;
                    window.location.href = strUrl;
                } else {
                    alert(msg);
                }
            }
        });

    },
    editDemandInfo: function() {
        var _this = this;
        if (!this.currentCustomerInfo.HouseCode)
            delete this.currentCustomerInfo.HouseCode
        Chenxin.ajax({
            type: "get",
            dataType: "text",
            paramType: 'json',
            url: "service/customerMsgr.ashx",
            data: { op: 'checkcustomerdemendpopwind', model: this.currentCustomerInfo },
            onSuccess: function(data, msg) {
                if (data.permit) {
                    var strUrl = "CustomerInfoAdd.aspx?infoclass=demandInfo";
                    strUrl = strUrl + "&customerid=" + _this.currentCustomerInfo.CustomerId;
                    window.location.href = strUrl;
                } else {
                    alert(msg);
                }
            }
        });

    },
    addFavorite: function() {
        Chenxin.ajax({
            type: "get",
            dataType: "text",
            paramType: 'json',
            url: "service/favorite.ashx",
            data: { op: 'add', favortype: '1', houseid: this.currentCustomerInfo.CustomerId, houseaddress: this.currentCustomerInfo.CustomerName },
            onSuccess: function(data, msg) {
                alert(msg);
            }
        });
    },
    createTraceWin:function(){
		_this = this;
		if (!this.currentTraceWin) {
            this.currentTraceWin = new Chenxin.UI.currentCustomerTraceForm({
                scrop: this,
                afterGetFormJson: function(json) {
                    delete json.AddPerson;
                    delete json.AddDepart;
                    json.HouseAddress = json.Address;
                    json.HouseId = _this.currentCustomerInfo.CustomerId;
                    json.HouseCode = _this.currentCustomerInfo.CustomerCode;
                    json.HouseAddress = _this.currentCustomerInfo.CustomerName;
                    json.status = 1;
                    delete json.Address;
                    delete json.CustomerCode;

                    if (this.isNotice) {
                        this.additionalParam.noticemodel = {
                            body: json.body,
                            NoticeTime: json.SendDate,
                            HouseId: _this.currentId,
                            HouseAddress: _this.currentCustomerInfo.CustomerName
                        };
                        delete json.body;
                        delete json.SendDate;
                    } else {
                        this.additionalParam = {};
                    }
                    this.additionalParam.isnotice = json.isnotice || '0';
                    delete json.isnotice;
                }
            });
        }
	},
	showCurrentTraceWin: function() {
        var _this = this;
		this.createTraceWin();
        this.currentTraceWin.show();
        $('#tr_noticetime').hide();
        $('#tr_noticecontent').hide();
        var _data = {};
        Chenxin.apply(_data, this.currentCustomerInfo);
        _data.HouseCode = this.currentCustomerInfo.CustomerCode;
        _data.AddPerson = this.currentCustomerInfo.OprPerson;
        _data.AddDepart = this.currentCustomerInfo.OprDepart;
        this.currentTraceWin.fitData(_data);
        this.currentTraceWin.setTitle('客源跟单');
    },
    chgPubLevel: function() {
        var objPage = this;
        if (!this.chgPubLevelForm) {
            this.chgPubLevelForm = new Chenxin.UI.PubCustomerForm({
                actionUrl: 'service/customerMsgr.ashx',
                title: '客需公开级别变更',
                alwaysCheckPermit: true,
                autoCheckPermit: true,
                afterGetFormJson: function(json) {
                    json.DemandId = json.CustomerId;
                    if (objPage.updCustomer) {
                        json.CustomerId = objPage.currentCustomerInfo.CustomerId;
                    } else {
                        delete json.CustomerId;
                    }
                    delete json.CustomerName;
                    delete json.Phone;
                    delete json.Mobile;
                    delete json.OprPerson;
                    delete json.OprDepart;
                },
                beforeSave: function() {
                    var bRet = true;
                    //是否公开相应客户信息
                    if (confirm("客户信息公开到相应的级别吗？")) {
                        objPage.updCustomer = true;
                    }
                    return bRet;
                }
            });
        }
        if (this.chgPubLevelForm.show("", { DemandId: objPage.currentCustomerInfo.DemandId })) {
            var model = {};
            model.CustomerId = objPage.currentCustomerInfo.DemandId;
            model.CustomerName = objPage.currentCustomerInfo.CustomerName;
            model.OprPerson = objPage.currentCustomerInfo.OprPerson;
            model.OprDepart = objPage.currentCustomerInfo.OprDepart;
            model.PubLevel = objPage.currentCustomerInfo.PubLevel;
            model.Mobile = objPage.currentBaseInfo.Mobile;
            model.Phone = objPage.currentBaseInfo.Phone;
            this.chgPubLevelForm.fitData(model);
        }
    },
    createSearch: function() {
        return new Chenxin.UI.CustomerManageSearch({
            scope: this
        });
    },
    initComponent: function() {
        Chenxin.UI.CustomerManage.superclass.initComponent.call(this);
    }
});

/**
*查询面板
*/
Chenxin.UI.CustomerManageSearch = Chenxin.extend(Chenxin.UI.SearchCommon, {
    id: 'customer_search',
    initEvent: function() {
        var _this = this;
        $('#btn_search').click(function() {
            _this.scope.reloadGrid();
        });
        $('.a-qucik-search').click(function() {
            _this.scope.reloadGrid({ quicktype: this.id });
        });
    },
    createComboList: function() {
        //需求类型
        var demandkind = new Chenxin.UI.Combo({
            containerId: '#DemandKind',
            tbxWidth: 45,
            items: comboCommon.CustomerDemandType
        });
        //需求状态
        var status = new Chenxin.UI.Combo({
            containerId: '#Status',
            tbxWidth: 45,
            items: comboCommon.CustomerDemandStatus
        });
        //类别
        var catalog = new Chenxin.UI.Combo({
            containerId:"#Catalog",
            tbxWidth:40,
            items:comboCommon.catalog
        });
        //区域
        var demandzone = new Chenxin.UI.ParameterCombo({
			containerId: '#DemandZone',
			name: 'demandzone',
			key:'System.Region',
			inModelType:'txt',
			tbxWidth:45
		});
        //物业类型
        var demandbuilding = new Chenxin.UI.ParameterCombo({
			containerId: '#DemandBuilding',
			name: 'demandbuilding',
			key:'Customer.Demand.HouseType',
			inModelType:'txt',
			tbxWidth:45
		});
        //面积
        var demandarea = new Chenxin.UI.ParameterCombo({
			containerId: '#DemandArea',
			name: 'demandarea',
			key:'Customer.Demand.HouseArea',
			inModelType:'txt',
			tbxWidth:45
		});
        //欲购总价
        var totalprice = new Chenxin.UI.ParameterCombo({
			containerId: '#TotalPrice',
			name: 'totalprice',
			key:'Customer.Demand.BuyHouseTPrice',
			inModelType:'txt',
			tbxWidth:45
		});
        //欲购单价
        var unitprice = new Chenxin.UI.ParameterCombo({
			containerId: '#UnitPrice',
			name: 'unitprice',
			key:'Customer.Demand.BuyHouseUPrice',
			inModelType:'txt',
			tbxWidth:45
		});
        //公开级别
        var PubLevel = new Chenxin.UI.Combo({
            containerId:"#PubLevel",
            tbxWidth:45,
            colWidth:80,
            items:comboCommon.CustomerPubLevel
        });
        var combl = new Chenxin.UI.StructComboList();
        var structs = combl.comboList;
        structs.push(demandkind);
        structs.push(catalog);
        structs.push(status);
        structs.push(demandzone);
        structs.push(demandbuilding);
        structs.push(demandarea);
        structs.push(totalprice);
        structs.push(unitprice);
        structs.push(PubLevel);

        return structs;
    },
    initComponent: function() {
        Chenxin.UI.CustomerManageSearch.superclass.initComponent.call(this);
    }
});

/*
*类别窗体
*/
Chenxin.UI.CatalogForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/customerMsgr.ashx',
    id: 'jqmCatalog',
    renderTo: 'divCatalog',
    jqmWidth: 250,
    alwaysCheckPermit: true,
    title:"设置类别",
    op:"updcatalog",
    initEvent:function(){
        var objForm = this;
        /**************事件：类别更新事件*************/
        $("#btnUpdCatalog").click(function(){
            objForm.op="updcatalog";
            objForm.save();
        });
        /**************事件：关闭窗体事件*************/
        $("#btnCloseCatalog").click(function(){
            objForm.hide();
        });
    },
    saveSucc:function(){
        this.reloadGrid();
    },
    createForm: function() {
        return formCommon.CustomerCatalogType();
    },
    initComponent: function() {
        Chenxin.UI.CatalogForm.superclass.initComponent.call(this);
    }
});

$(document).ready(function() {
    var customerMgr = new Chenxin.UI.CustomerManage();
});



































































